import shutil
import os
import numpy as np
import scipy.io as sio

with open('run_check_eh1_comp0_time0','r',encoding='utf-8') as file:
    run_check_time0 = file.readlines()
    run_check_time0 = [s.strip() for s in run_check_time0]

with open('run_check_eh1_comp0_time1','r',encoding='utf-8') as file:
    run_check_time1 = file.readlines()
    run_check_time1 = [s.strip() for s in run_check_time1]

path = './Resultados_bootstrap'

try:
    os.mkdir(path)
    print("Folder %s created!" % path)
except FileExistsError:
    print("Folder %s already exists" % path)


for i in range(1,101):
    if run_check_time0[i-1] == 'SIM':
        shutil.copy2('batch_eh1_comp0_time0/eh1_comp0_time0_boot'+str(i)+'/ParEst_eh1_time0_Mex_nokids1.mat', path +'/ParEst_eh1_time0_Mex_nokids1_boot'+str(i)+'.mat')
        if run_check_time1[i-1] == 'SIM':
            shutil.copy2('batch_eh1_comp0_time1/eh1_comp0_time1_boot'+str(i)+'/ParEst_eh1_time1_Mex_nokids.mat', path +'/ParEst_eh1_time1_Mex_nokids_boot'+str(i)+'.mat')

    
###############################################################################    
##junta todos os resultados em um arquivo .mat unico (ParEst_eh0_time0_Mex_nokids_allboots.mat e ParEst_eh0_time1_Mex_nokids_allboots.mat)

matrix_t0 = np.zeros((100,24))
matrix_t1 = np.zeros((100,5))

for j in range(1,101):
    if run_check_time0[j-1] == 'SIM':
        x_t0 = sio.loadmat(path + '/ParEst_eh1_time0_Mex_nokids1_boot'+str(j))
        x_t0 = x_t0['ParEst_eh1_time0_Mex_nokids1']
        matrix_t0[j-1] = x_t0
        if run_check_time1[j-1] == 'SIM':
            x_t1 = sio.loadmat(path + '/ParEst_eh1_time1_Mex_nokids_boot'+str(j))
            x_t1 = x_t1['ParEst_eh1_time1_Mex_nokids']
            matrix_t1[j-1] = x_t1
            
##As linhas referentes a pastas que nao rodaram ficam com 0
##O codigo a seguir remove as linhas com 0

matrix_t0 = np.delete(matrix_t0, np.where(matrix_t0 == np.zeros(24))[0], axis = 0)
matrix_t1 = np.delete(matrix_t1, np.where(matrix_t1 == np.zeros(5))[0], axis = 0)

###
## Adicionar coluna indicando qual o numero da pasta referente a cada resultado:


boot_num_t0 = np.zeros((100,1))
boot_num_t1 = np.zeros((100,1))

for k in range(1,101):
    if run_check_time0[k-1] == 'SIM':
        boot_num_t0[k-1] = k
    if run_check_time1[k-1] == 'SIM':
        boot_num_t1[k-1] = k

boot_num_t0 = np.delete(boot_num_t0, np.where(boot_num_t0 == 0)[0], axis = 0) ##deletando os zeros 
boot_num_t1 = np.delete(boot_num_t1, np.where(boot_num_t1 == 0)[0], axis = 0)

matrix_t0 = np.hstack((matrix_t0, boot_num_t0))
matrix_t1 = np.hstack((matrix_t1, boot_num_t1))


###############################################################################
    
mdic_t0 = {'__header__': b'MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Sat Aug 12 00:23:53 2023',
 '__version__': '1.0',
 '__globals__': [],
 'ParEst_eh1_time0_Mex_nokids1_allboots': matrix_t0}
    
mdic_t1 = {'__header__': b'MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Wed Aug 16 13:21:28 2023',
 '__version__': '1.0',
 '__globals__': [],
 'ParEst_eh0_time1_Mex_nokids_allboots': matrix_t1}

sio.savemat('ParEst_eh1_time0_Mex_nokids1_allboots.mat', mdic_t0)
sio.savemat('ParEst_eh1_time1_Mex_nokids_allboots.mat', mdic_t1)